<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="消杀区域" prop="area">
        <!-- <el-input v-model="queryParams.queryParams"/> -->
        <el-select v-model="queryParams.area" placeholder="请选择消杀区域">
          <el-option
            v-for="item in areaLists"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规范开始时间" prop="startDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.startDate"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="规范结束时间" prop="endDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.endDate"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="发布时间" prop="addTime">-->
<!--        <el-date-picker-->
<!--          v-model="dateRange"-->
<!--          size="small"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['oa:report:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="设备名称" align="center" prop="year" />-->
      <el-table-column label="消杀区域" align="center" prop="areaName" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="消杀内容" align="center" prop="content" />
      <el-table-column label="消杀频率" align="center" prop="frequency" />
      <el-table-column label="消杀时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件内容" align="center" prop="fileName">
        <template slot-scope="scope">
          <span @click="getDownLink(scope.row)">{{scope.row.fileName}}</span>
<!--          <span v-html="getDownLink(scope.row)"></span>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.readTime == null"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button v-if="scope.row.readTime == null"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
          >删除</el-button>
<!--          <el-button v-if="scope.row.readTime != null"-->
<!--                     size="mini"-->
<!--                     type="text"-->
<!--                     icon="el-icon-view"-->
<!--                     @click="handleView(scope.row)"-->
<!--                     v-hasPermi="['oa:report:list']"-->
<!--          >详细</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作汇报对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <!-- <div style="text-align: right;margin-top: -19px;margin-bottom: 19px;"><el-button
        type="primary"
        icon="el-icon-upload2"
        size="mini"
        @click="keyUpload"
      >一健提交上次消杀记录</el-button></div> -->
      <el-form ref="form" :model="form" :rules="rules" :disabled="form.disable" label-width="80px">
        <el-form-item label="消杀区域" prop="area">
          <el-select v-model="form.area" placeholder="请选择消杀区域">
          <el-option
            v-for="item in areaLists"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
<!--        <el-form-item label="汇报对象" prop="toUser">-->
<!--          <el-select v-model="form.toUser" filterable placeholder="请输入姓名选择">-->
<!--            <el-option-->
<!--              v-for="item in teacherList"-->
<!--              :key="item.userId"-->
<!--              :label="item.name"-->
<!--              :value="item.userId">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="消杀内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="消杀频率" prop="frequency">
          <el-input v-model="form.frequency" type="number" placeholder="请输入消杀频率" />
        </el-form-item>
        <el-form-item label="消杀时间" prop="createDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.createDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择消杀时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文件附件" prop="fileName">
          <span @click="getDownLink(form)" v-if="form.fileName != null">{{form.fileName}}</span>
          <el-upload
            ref="upload"
            :limit="1"
            accept=".doc, .docx, .jpg, .jpeg, .png,.pdf"
            :headers="upload.headers"
            :action="upload.url "
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或
              <em>点击上传</em>
            </div>
            <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许上传文档或者图片文件！</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportReport } from "@/api/oa/report";
import { getToken } from "@/utils/auth";
import {
  addDisinfect,
  delDisinfect,
  downloadFile,
  editDisinfect,
  lastreport,
  selectDisinfectList,
  areaList
} from "@/api/base/gardensystem";
import {downLoadword} from "@/utils/zipdownload";

export default {
  name: "xsgfgl",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作汇报表格数据
      reportList: [],


      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolCode:this.$store.getters.schoolCode,
      },
      // 日期范围
      dateRange: [],
      year: 2020,
      // 表单参数
      form: {},
      // 上传参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload"
      },
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年度不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        toUser: [
          { required: true, message: "汇报对象不能为空", trigger: "blur" }
        ],
      },
      areaLists:[]
    };
  },
  created() {
    this.getList();
    this.getAreaList()
  },
  methods: {
    /**一键上传**/
    async keyUpload(){
      try {
        const { data }=await lastreport()
        data.id=null
        this.form=data
      }catch (e) {
        console.log(e)
      }
    },
    /** 查询工作汇报列表 */
    async getList() {
      try{
        this.loading = true;
        const data=await selectDisinfectList(this.queryParams)
        this.reportList = data.rows;
        this.total = data.total;
        this.loading = false;

      }catch (e) {
        console.log(e)
      }
    },
    /** 下载附件链接 */
    getDownLink(row) {
      if(row.fileName && row.fileUrl){
        downLoadword('/common/eos/download?fileName='+row.fileName,row.fileName)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        area: null,
        content: null,
        fileUrl: null,
        fileName: null,
        frequency: null,
        way: null,
        createDate:null,
        schoolCode:this.$store.getters.schoolCode,
      };
      this.resetForm("form");
    },
  async  getAreaList(){
   const {rows}  = await areaList()
   this.areaLists = rows
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消杀规范";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = JSON.parse(JSON.stringify(row));
      this.form.area =  Number(row.area)
      console.log(this.form.area,'---------this.form.area');
      this.open = true;
      this.title = "修改消杀规范";
    },
    /** 详细按钮操作 */
    // handleView(row){
    //   this.reset();
    //   const id = row.id || this.ids
    //   getReport(id).then(response => {
    //     this.form = response.data;
    //     this.form.disable = true;
    //     this.open = true;
    //     this.title = "查看工作汇报";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            editDisinfect(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDisinfect(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除消杀编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDisinfect(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工作汇报数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportReport(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.form.fileUrl = response.url
      this.form.fileName = response.fileName
      this.$refs.upload.clearFiles();
    },
  }
};
</script>
<style lang="scss" scoped>
span{
  cursor: pointer;
}
</style>
